package com.picture;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class FrameLayoutTestActivity extends Activity {
	int lastx;//手指在屏幕上抬起时的位置x
	int lasty;//手指在屏幕上抬起时的位置y
	private BrowseView test;
	private BrowseView test1;
	private ImageView love;
	private ImageView love2;
	

	private Matrix currentMatrix = new Matrix();
	private Matrix currentMatrix1 = new Matrix();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.framelayout_test_activity);
		
		findViewById(R.id.bg).setDrawingCacheEnabled(true);
		
		test = (BrowseView) findViewById(R.id.test);
		test1 = (BrowseView) findViewById(R.id.test1);
		love = (ImageView) findViewById(R.id.love);
		love2 = (ImageView) findViewById(R.id.love2);
		

		currentMatrix.postScale(1f, 1f);
		test.setImageMatrix(currentMatrix);
		test.setis_Editable(true);
		
		currentMatrix1.postScale(1f, 1f);
		test1.setImageMatrix(currentMatrix1);
		test1.setis_Editable(true);

//		final Bitmap bitmap = ((BitmapDrawable) (love.getDrawable())).getBitmap();  
//		love.setOnTouchListener(new OnTouchListener(){  
//            @Override  
//            public boolean onTouch(View arg0, MotionEvent arg1)   
//            {  
//                // TODO Auto-generated method stub
//            	int x = (int)arg1.getX();
//            	int y = (int)arg1.getY();
//            	//点击到控件区域
//            	if ((x > love.getLeft() && x < love.getRight())
//            			&& (y > love.getTop() && x < love.getBottom())) {
//
//                    if (bitmap.getPixel((int)(arg1.getX() - love.getLeft()),((int)(arg1.getY() - love.getTop())))==0){ 
//                        return false;//透明区域返回true  
//                    } 
//            	} 
//                return true;  
//            }             
//        });
		
//		love.setOnTouchListener(new OnTouchListener() {
//					int startx;//手指第一次点击屏幕时的位置x
//					int starty;//手指第一次点击屏幕时的位置y
//					public boolean onTouch(View v, MotionEvent event) {
//							switch (event.getAction()) {
//							case MotionEvent.ACTION_DOWN:
//								startx = (int) event.getRawX();
//								starty = (int) event.getRawY();
//								break;
//							case MotionEvent.ACTION_UP:
//								lastx = test.getLeft();
//								System.out.println("lastx:"+lastx);
//								lasty = test.getTop();
//								System.out.println("lasty:"+lasty);
//								break;
//							case MotionEvent.ACTION_MOVE:
//								int x= (int) event.getRawX();
//								int y= (int) event.getRawY();
//								int dx = x-startx;
//								int dy = y-starty;
//								test.layout(test.getLeft()+dx, test.getTop()+dy, test.getRight()+dx, test.getBottom()+dy);
//								startx = (int) event.getRawX();
//								starty = (int) event.getRawY();
//								test.invalidate();
//								break;
//							}
//							return true;
//						}
//				});
	}
	public void onClick(View view){
		findViewById(R.id.bg).setDrawingCacheEnabled(true);
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.lvoe);
		Bitmap phto = BitmapFactory.decodeResource(getResources(), R.drawable.test);
//		Bitmap phto = ((BitmapDrawable)(test.getDrawable())).getBitmap();
		love2.setImageBitmap(montage(bmp,phto,lastx,lasty));
	}
	/*
	 * 获取将两张组合在一起的新图片
	 */
	public Bitmap montage(Bitmap xiangkuang,Bitmap phto,int phtoX,int phtoY){
//		Bitmap newBitmap = Bitmap.createBitmap(xiangkuang.getWidth(), xiangkuang.getHeight(), Config.ARGB_8888);  
//		Canvas canvas = new Canvas(newBitmap);
//		canvas.setMatrix(test.getImageMatrix());
//		canvas.drawBitmap(phto, phtoX, phtoY, null);
//		canvas.setMatrix(null);
//		canvas.drawBitmap(xiangkuang, 0, 0, null);
//		return newBitmap;
		Bitmap newBitmap = Bitmap.createBitmap(findViewById(R.id.bg).getDrawingCache());
		findViewById(R.id.bg).setDrawingCacheEnabled(false);
		return newBitmap;
	}
}
